package com.cl.short_video_java.config;

import org.modelmapper.AbstractConverter;
import org.modelmapper.Converter;
import org.modelmapper.ModelMapper;
import org.modelmapper.convention.MatchingStrategies;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author 付
 * @Date 2024-11-29 11:30
 * @Version 1.0
 * @description
 * @since 1.0
 */
@Configuration
public class ModelMapperConfig {

	private Converter<Date, String> dateToStringConverter = new AbstractConverter<Date, String>() {
		@Override
		protected String convert(Date date) {
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

			return date == null ? null : simpleDateFormat.format(date);
		}
	};

	/**
	 * 将ModelMapper注册到Spring中
	 * 可以添加一些定制化的配置，官方文档：http://modelmapper.org/user-manual/property-mapping/
	 *
	 * @return org.modelmapper.ModelMapper
	 * @author flyduck
	 * @date 2020/11/25 21:35
	 */
	@Bean
	public ModelMapper modelMapper() {
		ModelMapper modelMapper = new ModelMapper();

		// 官方配置说明： http://modelmapper.org/user-manual/configuration/
		// 完全匹配
		modelMapper.getConfiguration().setFullTypeMatchingRequired(true);

		// 匹配策略使用严格模式
		modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);

		modelMapper.addConverter(dateToStringConverter);
		return modelMapper;
	}
}
